var express = require('express');

var app = express();
var moappDb = require("./libs/db")

var appConfig = {
    routes: [{
        method: "get",
        key: "/",
        handler: (req, res) => {
            res.send('Hello World');
        }
    }, {
        method: "get",
        key: "/moappdb",
        handler: (req, res) => {
            var sqlPath = req.query.key;
            var ps = req.query;
            moappDb.moappDb.queryBySqlFile(sqlPath, ps, function(result) {
                var responseText = (result && result != null) ? JSON.stringify(result) : "error";
                res.send(responseText);
            })
        }
    }, ]
}

appConfig.routes.forEach(route => {
    if (route.method == "get") {
        app.get(route.key, function(req, res) {
            route.handler(req, res);
        });
    }
})
var server = app.listen(9000, () => {
    var host = server.address().address
    var port = server.address().port
    console.log("应用实例，访问地址为 http://%s:%s", host, port)
});